﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BW.Mes.WinForms.Production.ProductionCommon
{
    /// <summary>
    /// http请求封装类
    /// </summary>
    public static class ProduceHttpClientHelper
    {
        public async static Task<string> RequestUrl(ProduceRequestType method,string url,string data="")
        {
            using (HttpClient client=new HttpClient())
            {
                client.BaseAddress=new Uri("http://120.26.197.100:8022");
                HttpResponseMessage response=null;

                HttpContent httpContent=null;
                //判断data是否为空  
                if (!string.IsNullOrEmpty(data))
                {
                    httpContent = new StringContent(data, Encoding.UTF8, "application/json");
                }

                switch (method)
                {
                    case ProduceRequestType.get:
                        response= await client.GetAsync(url);
                        break;
                    case ProduceRequestType.post:
                        response=await client.PostAsync(url, httpContent);
                        break;
                    case ProduceRequestType.put:
                        response = await client.PutAsync(url, httpContent);
                        break;
                    case ProduceRequestType.delete:
                        response = await client.DeleteAsync(url);
                        break;
                }
                //判断状态码是否成功
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadAsStringAsync();
                }
                //返回空
                return string.Empty;
            }
        }
    }
}
